<template>
  <div :style="backgroundDiv">
    <div class="main">
      <!-- <lang-select class="right-menu-item hover-effect" /> -->
      <div class="box">
        <div class="content">
          <div class="people">
            <img :src="imgSrc" width="100%" height="100%" alt="">
          </div>
          <div class="permission-container">
            <div class="permission-form" :class="{'hall': platform === 'hall'}">
              <div class="logo">
                <img :src="logoSrc" height="100%" alt="">
              </div>
              <div class="text">{{ $t('login.hall') }}</div>
              <el-row>
                <el-col align="left" class="gl_my_20">
                  <span class="title">
                    {{ $t('login.forgetPwd') }}
                  </span>
                </el-col>
              </el-row>
              <forget-password ref="refForget">
                <template v-slot:FORGETWORD>
                  <el-form :model="repasswordForm" @keydown.enter.prevent="return false;">
                    <el-form-item prop="repassword" :rules="rules.repassword">
                      <span class="svg-container">
                        <svg-icon icon-class="password" />
                      </span>
                      <el-input ref="repassword" :key="repasswordType" v-model="repasswordForm.repassword" :type="repasswordType === 'repassword' ? 'password': 'text'" :placeholder="$t('rules.confirm')" name="repassword" tabindex="2" />
                      <span class="show-pwd" @click="showrePwd">
                        <svg-icon :icon-class="repasswordType === 'repassword' ? 'eye' : 'eye-open'" />
                      </span>
                    </el-form-item>
                  </el-form>
                </template>
                <template v-slot:FORGET>
                  <el-button type="primary" class="gl_button_long" :loading="loading" @click.native.prevent="submitForget">{{ $t('forget.title') }}</el-button>
                </template>
              </forget-password>
              <el-row>
                <el-col align="right">
                  <el-button type="text" @click="routerRedirect('login')">{{ $t('register.goLogin') }}</el-button>
                </el-col>
              </el-row>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="archival_copyright">
      <div class="archival">版权所有 © {{ copyright }} <a :href="archivalUrl">苏ICP备{{ archival }}号</a></div>
      <div class="copyright">
        <a :href="archivalUrl">
          <img :src="archivalImg" alt="">
          苏公网安备 {{ archival }}号
        </a>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState, mapActions } from 'vuex';
// import LangSelect from '@/components/LangSelect';
import { api_POST_UsersRegisterResetPassword } from '@/extend/api/platform/register';
import ForgetPassword from '@/plugin/ForgetPassword/forgetPassword.vue';
import archivalImg from '@/assets/images/beian.png';
import { RegisterDto_Rules } from '@/lib/validate/register/index.js';
export default {
  components: {
    // LangSelect,
    ForgetPassword
  },
  props: {
    platform: {
      type: String,
      default: 'game'
    }
  },
  data() {
    let _bgUrl = '';
    if (this.platform === 'game') {
      _bgUrl = require('@/assets/images/bg.png');
    } else {
      _bgUrl = require('@/assets/images/bgHall.png');
    }
    return {
      loading: false,
      backgroundDiv: {
        backgroundImage: 'url(' + _bgUrl + ')',
        backgroundRepeat: 'no-repeat',
        backgroundSize: '100% 100%'
      },
      bgSrc: _bgUrl,
      imgSrc: '',
      logoSrc: require('@/assets/images/logo/logo_word.png'),
      archivalImg: archivalImg,
      repasswordForm: {
        repassword: ''
      },
      repasswordType: 'repassword',
      rules: {
        repassword: new RegisterDto_Rules()['password']
      }
    };
  },
  computed: {
    ...mapState({
      archival: state => state.app.archival,
      archivalUrl: state => state.app.archivalUrl,
      copyright: state => state.app.copyright,
      copyrightUrl: state => state.app.copyrightUrl
    })
  },
  mounted() {},
  methods: {
    showrePwd() {
      if (this.repasswordType === 'repassword') {
        this.repasswordType = '';
      } else {
        this.repasswordType = 'repassword';
      }
    },
    ...mapActions({ setMarkTelephone: 'app/setMarkTelephone' }),
    // 找回密码按钮
    submitForget() {
      if (
        this.$refs.refForget['forgetDto'].password !==
        this.repasswordForm.repassword
      ) {
        this.$message.error('两次输入密码不一致！');
        return false;
      }
      this.$refs.refForget.$refs['forgetForm'].validate(valid => {
        if (valid) {
          this.loading = true;
          this.$refs.refForget['forgetDto'].isNew = false; // isNew - true: 注册新用户；false: 已有用户（找回密码）
          api_POST_UsersRegisterResetPassword(this.$refs.refForget['forgetDto'])
            .then(res => {
              this.loading = false;
              if (res.result === 'Success') {
                this.$message({ message: '操作成功！', type: 'success' });
                this.setMarkTelephone(
                  this.$refs.refForget['forgetDto'].phoneNumber
                );
                this.$router.push({
                  name: 'login',
                  query: {
                    ReturnUrl: this.$route.query['ReturnUrl']
                  },
                  params: this.$route.params
                });
              } else if (res.result === 'AlreadyExists') {
                this.$message({ type: 'warning', message: '用户已经存在' });
              } else if (res.result === 'InvalidVerificationCode') {
                this.$message({ type: 'warning', message: '无效的验证码' });
              }
            })
            .catch(error => {
              this.loading = false;
              this.$message({ type: 'warning', message: error.error.message });
            });
        } else {
          return false;
        }
      });
    },
    routerRedirect(path) {
      // 返回登录
      this.$router.push({
        name: path,
        query: this.$route.query,
        params: this.$route.params
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.box {
  width: 700px;
  min-width: 600px;
  min-height: 500px;
}
.content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  .people {
    width: 45%;
    min-width: 200px;
  }
}
.right-menu-item {
  float: right;
  padding: 20px 40px;
  height: 100%;
  font-size: 26px;
  color: #fff;
  vertical-align: text-bottom;

  &.hover-effect {
    cursor: pointer;
    transition: background 0.3s;

    &:hover {
      background: rgba(0, 0, 0, 0.025);
    }
  }
}
.main {
  min-height: calc(100vh - 70px);
  display: flex;
  align-items: center;
  justify-content: center;
}
.archival_copyright {
  height: 70px;
  padding-top: 10px;
  line-height: 30px;
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.archival_copyright a {
  transition: all 0.3s ease;
}
.archival_copyright a:hover {
  text-decoration: underline;
}
.copyright img {
  vertical-align: middle;
  margin-right: 2px;
  width: 18px;
}
</style>
